using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;

namespace Galgame
{
    public class SceneLoader : MonoBehaviour
    {
        public Image fadePanel;
        public float fadeDuration = 1.0f;

        void Start()
        {
            StartCoroutine(FadeIn());
        }

        public void LoadMainScene()
        {
            StartCoroutine(FadeOut(1));
        }

        IEnumerator FadeIn()
        {
            fadePanel.gameObject.SetActive(true);
            float timer = fadeDuration;
            while (timer > 0)
            {
                fadePanel.color = new Color(0, 0, 0, timer / fadeDuration);
                timer -= Time.deltaTime;
                yield return null;
            }
            fadePanel.gameObject.SetActive(false);
        }

        IEnumerator FadeOut(int sceneIndex)
        {
            fadePanel.gameObject.SetActive(true);
            float timer = 0;
            while (timer < fadeDuration)
            {
                fadePanel.color = new Color(0, 0, 0, timer / fadeDuration);
                timer += Time.deltaTime;
                yield return null;
            }
            SceneManager.LoadScene(sceneIndex);
        }
        public void QuitGame()
        {
            #if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
            #else
            Application.Quit();
            #endif
        }
    }
}
